package com.enjoy.adv.server;

import com.enjoy.adv.client.HeartBeatReqHandler;
import com.enjoy.adv.kryocodec.kryoDecoder;
import com.enjoy.adv.kryocodec.kryoEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.ReadTimeoutHandler;

/**
 * Created by HP on 2019/7/19.
 */
public class ServerInit extends ChannelInitializer<SocketChannel>{
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        /*Netty提供的日志打印Handler，可以展示发送接收出去的字节*/
        //ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));

        //剥离接受道德消息的长度字段 拿到实际的消息报文的字节数据
       ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(65535,0,2,0,2))
               //发出去的消息增加字节长度
               .addLast(new LengthFieldPrepender(2))
               //反序列化
               .addLast(new kryoDecoder())
               //序列化
               .addLast(new kryoEncoder())
               //超时检测
               .addLast(new ReadTimeoutHandler(50))
               //登录应答
               .addLast(new LoginAuthRespHandler())
               //心跳应答
               .addLast(new HeartBeatRespHandler())
               //业务处理
               .addLast(new ServerBusiHandler());
    }
}
